package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.domain.entity.ApplyRefund;
import com.spa.infrastructure.mapper.ApplyRefundMapperStruct;
import com.spa.infrastructure.repository.condition.ApplyRefundCondition;
import com.spa.infrastructure.repository.mapper.ApplyRefundMapper;
import com.spa.infrastructure.repository.po.ApplyRefundPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class ApplyRefundRepositoryService extends ServiceImpl<ApplyRefundMapper, ApplyRefundPO> {

    @Autowired
    private ApplyRefundMapper applyRefundMapper;

    public Long add(ApplyRefundPO po){
        applyRefundMapper.insert(po);
        return po.getId();
    }

    public Page<ApplyRefund> page(ApplyRefundCondition condition) {
        LambdaQueryWrapper<ApplyRefundPO> wrapper = wrapper(condition);
        Page<ApplyRefundPO> pos = applyRefundMapper.selectPage(Page.of(condition.getCurrent(), condition.getSize()), wrapper);
        return ApplyRefundMapperStruct.INSTANCE.po2EntityPage(pos);
    }

    public ApplyRefund getApplyRefund(ApplyRefundCondition condition) {
        LambdaQueryWrapper<ApplyRefundPO> wrapper = wrapper(condition);
        ApplyRefundPO po = applyRefundMapper.selectOne(wrapper);
        return ApplyRefundMapperStruct.INSTANCE.po2Entity(po);
    }

    public List<ApplyRefund> list(ApplyRefundCondition condition) {
        LambdaQueryWrapper<ApplyRefundPO> wrapper = wrapper(condition);
        List<ApplyRefundPO> pos = applyRefundMapper.selectList(wrapper);
        return ApplyRefundMapperStruct.INSTANCE.po2EntityList(pos);
    }

    private LambdaQueryWrapper<ApplyRefundPO> wrapper(ApplyRefundCondition condition) {
        LambdaQueryWrapper<ApplyRefundPO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollUtil.isNotEmpty(condition.getStatusList()), ApplyRefundPO::getStatus, condition.getStatusList());
        return wrapper;
    }

}
